package com.foreknow.dao.impl;

import com.foreknow.bean.Guestbook;
import com.foreknow.dao.GuestbookDao;
import com.foreknow.mapping.EntityMapping;
import com.foreknow.mapping.MappingFactory;

import java.sql.SQLException;
import java.util.List;

public class GuestbookDaoImpl extends BaseDao implements GuestbookDao {
    @Override
    public List<Object> getQueryAll() throws SQLException {
        String sql = "select * from guestbook";
        EntityMapping mapping = mappingFactory.getMap(MappingFactory.GUESTBOOK_MAPPING);
        List<Object> list = jdbcTemplate.query(sql,mapping);
        return list;
    }

    public boolean add(Guestbook guestbook) throws SQLException{
        String sql = "insert into guestbook(id,name,phone,email,title,content,createdtime) values(?,?,?,?,?,?,?)";
        int x = jdbcTemplate.update(sql,guestbook.getId(),guestbook.getName(),guestbook.getPhone(),guestbook.getEmail(),guestbook.getTitle(),guestbook.getContent(),guestbook.getCreatedtime());
        return x == 1;
    }

    public boolean delete(Integer id) throws SQLException{
        String sql = "delete from guestbook where id=?";
        int isRight = jdbcTemplate.update(sql,id);
        return isRight==1;
    }
}
